package cn.zysuyuan.item.controller;

import cn.zysuyuan.common.entity.PageResult;
import cn.zysuyuan.common.entity.R;
import cn.zysuyuan.item.entity.ProductBase;
import cn.zysuyuan.item.entity.ProductBaseImg;
import cn.zysuyuan.item.service.ProductBaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author flypig
 * @version 1.0
 * @date 2019-11-22 20:21
 */

@Api(tags = "产地产区管理")
@RestController
@RequestMapping("productBase")
public class ProductBaseController {

    @Autowired
    private ProductBaseService productBaseService;

    @ApiOperation(value = "产地产区基地列表管理", notes = "产地产区基地列表管理")
    @GetMapping("productBasedata")
    public ResponseEntity<PageResult<ProductBase>> select(@RequestParam Map<String,Object> map ){
        PageResult<ProductBase> result = this.productBaseService.selectList(map);
        if (CollectionUtils.isEmpty(result.getRows())) {
            List<ProductBase> list = new ArrayList<ProductBase>();
            result.setRows(list);
            return ResponseEntity.ok(result);
        }
        return ResponseEntity.ok(result);
    }

    @ApiOperation(value = "查看所有产地名称", notes = "查看所有产地名称")
    @GetMapping("productBaseName")
    public ResponseEntity<List<ProductBase>> selectAll() {
        List<ProductBase> list = productBaseService.selectAllProductName();
        return ResponseEntity.ok(list);
    }

    @ApiOperation(value = "保存产地产区基地信息",notes = "保存产地产区基地信息")
    @PostMapping("productBasedata")
    public R save(@RequestBody ProductBase productBase) {
        System.out.println("开始添加");
        if (productBaseService.insert(productBase) > 0) {
            Map<String,Object> map = new HashMap<>();
            map.put("flag",true);
            return R.ok(map);
        }else {
            return R.error();
        }
    }

    @ApiOperation(value = "更新基地信息", notes = "更新基地信息")
    @PutMapping("productBasedata")
    public R update(@RequestBody ProductBase productBase) {
        if (productBaseService.update(productBase) > 0){
            Map<String,Object> map = new HashMap<>();
            map.put("flag",true);
            return R.ok(map);
        } else {
            return R.error();
        }
    }

    @ApiOperation(value = "删除基地信息",notes = "删除基地信息")
    @DeleteMapping("productBasedata")
    public R delete(@RequestParam("id") String id) {
        productBaseService.delete(id);
        return R.ok();
    }

    @ApiOperation(value = "获取基地照片", notes = "获取基地照片")
    @GetMapping("productImg")
    public ResponseEntity<PageResult<ProductBaseImg>> selectListImg(@RequestParam ("id") String id){
        PageResult result = this.productBaseService.selectListImg(id);
        return ResponseEntity.ok(result);
    }

    @ApiOperation(value = "获取全部药材名称", notes = "获取全部药材名称")
    @GetMapping("medicinalName")
    public ResponseEntity<List<String>> selectAllMedicinal() {
        List<String> result = this.productBaseService.selectAllMedicinal();
        return ResponseEntity.ok(result);
    }

    @ApiOperation(value = "保存基地照片", notes = "保存基地照片")
    @PostMapping("productImg")
    public ResponseEntity<Map<String,Object>> uploadImg(@RequestParam("files")MultipartFile[] files, HttpServletRequest request, @RequestParam Map<String, Object> map) {
        return ResponseEntity.status(HttpStatus.CREATED).body(productBaseService.saveImg(files,request,map));
    }

}
